package com.tencent.sr.iris.basic.saas.restapi;

import com.tencent.sr.iris.basic.saas.dependency.NoticeDependency;
import com.tencent.sr.iris.basic.saas.dependency.WxDependency;
import com.tencent.sr.iris.basic.saas.service.dto.request.PreviewRequest;
import com.tencent.sr.iris.basic.service.interfaces.request.GetWxQrCodeRequest;
import com.tencent.sr.iris.basic.service.interfaces.request.NoticeBaseRequest;
import com.tencent.sr.iris.basic.service.interfaces.request.NoticePageRequest;
import com.tencent.sr.iris.basic.service.interfaces.request.NoticeSaveRequest;
import com.tencent.sr.iris.basic.service.interfaces.response.NoticeDetailResponse;
import com.tencent.sr.iris.basic.service.interfaces.response.NoticePageResponse;
import com.tencent.sr.iris.basic.service.interfaces.response.QrCodeBase64Response;
import com.tencent.sr.rmall.common.exception.retail.TRetailBizException;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import com.tencent.sr.rmall.common.request.PaginationRequest;
import com.tencent.sr.rmall.common.response.PaginationResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.Optional;

@RestController
@Slf4j
@Api("官方公告")
@RequestMapping("/iris/basic/saas/v1/notice")
public class NoticeController {

    @Resource
    private NoticeDependency noticeDependency;

    @Resource
    private WxDependency wxDependency;
    
    @Value("${miniprogram.env:release}")
    private String env;

    @ApiOperation("新增、编辑公告")
    @PostMapping("save")
    HttpResult<Boolean> save(@Valid @RequestBody NoticeSaveRequest request) {
        Boolean response = noticeDependency.save(request);
        return HttpResult.succ(response);
    }

    @ApiOperation("删除公告")
    @PostMapping("delete")
    HttpResult<Boolean> delete(@Valid @RequestBody NoticeBaseRequest request) {
        Boolean response = noticeDependency.delete(request);
        return HttpResult.succ(response);
    }

    @ApiOperation("发布公告")
    @PostMapping("publish")
    HttpResult<Boolean> publish(@Valid @RequestBody NoticeBaseRequest request) {
        Boolean response = noticeDependency.publish(request);
        return HttpResult.succ(response);
    }

    @ApiOperation("分页查询公告")
    @PostMapping("page")
    HttpResult<PaginationResponse<NoticePageResponse>> page(@Valid @RequestBody PaginationRequest<NoticePageRequest> request) {
        PaginationResponse<NoticePageResponse> response = noticeDependency.page(request);
        return HttpResult.succ(response);
    }

    @ApiOperation("分页公告详情")
    @PostMapping("detail")
    HttpResult<NoticeDetailResponse> detail(@Valid @RequestBody NoticeBaseRequest request) {
        NoticeDetailResponse response = noticeDependency.detail(request);
        return HttpResult.succ(response);
    }

    @ApiOperation("预览公告")
    @PostMapping("preview")
    HttpResult<QrCodeBase64Response> preview(@Valid @RequestBody PreviewRequest request) {
        NoticeBaseRequest req = new NoticeBaseRequest();
        req.setId(request.getId());
        NoticeDetailResponse detail = noticeDependency.detail(req);
        if(detail == null){
            throw new TRetailBizException("公告不存在");
        }
        Date validTime = detail.getValidTime();
        String content = detail.getContent();
        String name = detail.getName();
        String scene = String.format("name=%s&validTime=%s&content=%s", StringUtils.stripToEmpty(name),
                Optional.ofNullable(validTime).map(Date::getTime).orElse(null), content);

        GetWxQrCodeRequest qrCodeRequest = new GetWxQrCodeRequest();
        
        String envVersion = Optional.ofNullable(request.getEnvVersion()).orElse(env);
        qrCodeRequest.setEnvVersion(envVersion);
        
        qrCodeRequest.setPage("pages/user/announcement/detail/index");
        qrCodeRequest.setScene(scene);
        QrCodeBase64Response response = wxDependency.getWxQrCodeBase64(qrCodeRequest);
        return HttpResult.succ(response);
    }
    
}
